﻿using CoffeeShop.BIZ;
using CoffeeShop.Common;
using CoffeeShop.Models.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CoffeeShop.Controllers
{
    public class CategoryController : Controller
    {
        private CategoryBiz _categoryBiz = new CategoryBiz();

        /// <summary>
        /// List page
        /// </summary>
        [Authorize(Roles = "Admin")]
        public ActionResult Index()
        {
            List<Category> listCategory = new List<Category>();
            listCategory = _categoryBiz.GetListCategories();
            return View(listCategory);
        }

        /// <summary>
        /// Create a category
        /// </summary>
        [HttpPost]
        [Authorize(Roles = "Admin, Staff")]
        public string CreateCategory()
        {
            string categoryName = Request.Params["categoryName"].Trim();
            if (_categoryBiz.IsExistedCategoryAdd(categoryName))
            {
                return "ExistedCategory";
            }
            Category category = new Category();
            category = _categoryBiz.ConvertToBaseObject(categoryName);
            if (_categoryBiz.CreateCategory(category))
            {
                return "CreateSuccessful";
            }
            return "CreateFail";
        }

        /// <summary>
        /// Update a category
        /// </summary>
        [HttpPost]
        [Authorize(Roles = "Admin, Staff")]
        public string UpdateCategory()
        {
            // Get param value
            string categoryId = Request.Params["categoryId"].Trim();
            string categoryName = Request.Params["categoryName"].Trim();

            // If have any value null -> fail
            if (string.IsNullOrEmpty(categoryId) || string.IsNullOrEmpty(categoryName))
            {
                return "UpdateFail";
            }

            Category category = new Category();
            category = _categoryBiz.ConvertToBaseObject(categoryName, Convert.ToInt32(categoryId));

            if (_categoryBiz.IsExistedCategoryUpdate(category.ID, category.Name))
            {
                return "ExistedCategory";
            }

            if (_categoryBiz.UpdateCategory(category))
            {
                return "UpdateSuccessful";
            }

            return "UpdateFail";
        }

        /// <summary>
        /// Delete an area in Database
        /// </summary>
        [HttpPost]
        [Authorize(Roles = "Admin, Staff")]
        public string DeleteCategory(string categoryId)
        {
            if (_categoryBiz.DeleteCategory(Convert.ToInt32(categoryId)))
            {
                return "DeleteSuccessful";
            }
            return "FailDeleteCategory";
        }

        [HttpPost]
        [Authorize(Roles = "Admin, Staff")]
        public PartialViewResult LoadAllCategories()
        {
            List<Category> listAllCategories = new List<Category>();
            listAllCategories = _categoryBiz.GetListCategories();
            return PartialView("_CategoryList", listAllCategories);
        }

        /// <summary>
        /// Get list category to send value to object
        /// </summary>
        /// <returns></returns>
        public JsonResult GetListCategory()
        {
            List<Category> listCategory = new List<Category>();
            listCategory = _categoryBiz.GetListCategories();

            List<Object> listResult = new List<Object>();
            foreach (var category in listCategory)
            {
                var result = CmnFunc.ConvertDynamicObjectWithFullAttr(category);
                listResult.Add(result);
            }
            return Json(listResult, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetCategoryById(string categoryId)
        {
            Category category = new Category();
            category = _categoryBiz.GetCategoryById(Convert.ToInt32(categoryId));
            return Json(new { CategoryId = category.ID, CategoryName = category.Name, IsDeleted = category.IsDeleted }
                , JsonRequestBehavior.AllowGet);
        }
    }
}
